/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.vijayan.profile.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.vijayan.profile.NoSuchMyExpectationException;
import com.vijayan.profile.model.MyExpectation;
import com.vijayan.profile.model.impl.MyExpectationImpl;
import com.vijayan.profile.model.impl.MyExpectationModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the my expectation service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link MyExpectationUtil} to access the my expectation persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author vijayan
 * @see MyExpectationPersistence
 * @see MyExpectationUtil
 * @generated
 */
public class MyExpectationPersistenceImpl extends BasePersistenceImpl<MyExpectation>
	implements MyExpectationPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = MyExpectationImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countAll", new String[0]);

	/**
	 * Caches the my expectation in the entity cache if it is enabled.
	 *
	 * @param myExpectation the my expectation to cache
	 */
	public void cacheResult(MyExpectation myExpectation) {
		EntityCacheUtil.putResult(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationImpl.class, myExpectation.getPrimaryKey(),
			myExpectation);
	}

	/**
	 * Caches the my expectations in the entity cache if it is enabled.
	 *
	 * @param myExpectations the my expectations to cache
	 */
	public void cacheResult(List<MyExpectation> myExpectations) {
		for (MyExpectation myExpectation : myExpectations) {
			if (EntityCacheUtil.getResult(
						MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
						MyExpectationImpl.class, myExpectation.getPrimaryKey(),
						this) == null) {
				cacheResult(myExpectation);
			}
		}
	}

	/**
	 * Clears the cache for all my expectations.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(MyExpectationImpl.class.getName());
		EntityCacheUtil.clearCache(MyExpectationImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the my expectation.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(MyExpectation myExpectation) {
		EntityCacheUtil.removeResult(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationImpl.class, myExpectation.getPrimaryKey());
	}

	/**
	 * Creates a new my expectation with the primary key. Does not add the my expectation to the database.
	 *
	 * @param exceptationId the primary key for the new my expectation
	 * @return the new my expectation
	 */
	public MyExpectation create(long exceptationId) {
		MyExpectation myExpectation = new MyExpectationImpl();

		myExpectation.setNew(true);
		myExpectation.setPrimaryKey(exceptationId);

		return myExpectation;
	}

	/**
	 * Removes the my expectation with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the my expectation to remove
	 * @return the my expectation that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the my expectation with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param exceptationId the primary key of the my expectation to remove
	 * @return the my expectation that was removed
	 * @throws com.vijayan.profile.NoSuchMyExpectationException if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation remove(long exceptationId)
		throws NoSuchMyExpectationException, SystemException {
		Session session = null;

		try {
			session = openSession();

			MyExpectation myExpectation = (MyExpectation)session.get(MyExpectationImpl.class,
					new Long(exceptationId));

			if (myExpectation == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + exceptationId);
				}

				throw new NoSuchMyExpectationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					exceptationId);
			}

			return remove(myExpectation);
		}
		catch (NoSuchMyExpectationException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected MyExpectation removeImpl(MyExpectation myExpectation)
		throws SystemException {
		myExpectation = toUnwrappedModel(myExpectation);

		Session session = null;

		try {
			session = openSession();

			if (myExpectation.isCachedModel() || BatchSessionUtil.isEnabled()) {
				Object staleObject = session.get(MyExpectationImpl.class,
						myExpectation.getPrimaryKeyObj());

				if (staleObject != null) {
					session.evict(staleObject);
				}
			}

			session.delete(myExpectation);

			session.flush();
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationImpl.class, myExpectation.getPrimaryKey());

		return myExpectation;
	}

	public MyExpectation updateImpl(
		com.vijayan.profile.model.MyExpectation myExpectation, boolean merge)
		throws SystemException {
		myExpectation = toUnwrappedModel(myExpectation);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, myExpectation, merge);

			myExpectation.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
			MyExpectationImpl.class, myExpectation.getPrimaryKey(),
			myExpectation);

		return myExpectation;
	}

	protected MyExpectation toUnwrappedModel(MyExpectation myExpectation) {
		if (myExpectation instanceof MyExpectationImpl) {
			return myExpectation;
		}

		MyExpectationImpl myExpectationImpl = new MyExpectationImpl();

		myExpectationImpl.setNew(myExpectation.isNew());
		myExpectationImpl.setPrimaryKey(myExpectation.getPrimaryKey());

		myExpectationImpl.setExceptationId(myExpectation.getExceptationId());
		myExpectationImpl.setProfileId(myExpectation.getProfileId());
		myExpectationImpl.setMinAge(myExpectation.getMinAge());
		myExpectationImpl.setMaxAge(myExpectation.getMaxAge());
		myExpectationImpl.setExpectedReligion(myExpectation.getExpectedReligion());
		myExpectationImpl.setExpectedCaste(myExpectation.getExpectedCaste());
		myExpectationImpl.setExpectedSubCaste(myExpectation.getExpectedSubCaste());
		myExpectationImpl.setExpectedCountry(myExpectation.getExpectedCountry());
		myExpectationImpl.setExpectedState(myExpectation.getExpectedState());
		myExpectationImpl.setExpectedCity(myExpectation.getExpectedCity());
		myExpectationImpl.setExpectedHeight(myExpectation.getExpectedHeight());
		myExpectationImpl.setExpectedWeight(myExpectation.getExpectedWeight());
		myExpectationImpl.setExpectedComplexion(myExpectation.getExpectedComplexion());
		myExpectationImpl.setExpectedLanguage(myExpectation.getExpectedLanguage());
		myExpectationImpl.setExpectedMaritalStatus(myExpectation.getExpectedMaritalStatus());
		myExpectationImpl.setExpectationDetail(myExpectation.getExpectationDetail());

		return myExpectationImpl;
	}

	/**
	 * Finds the my expectation with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the my expectation to find
	 * @return the my expectation
	 * @throws com.liferay.portal.NoSuchModelException if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the my expectation with the primary key or throws a {@link com.vijayan.profile.NoSuchMyExpectationException} if it could not be found.
	 *
	 * @param exceptationId the primary key of the my expectation to find
	 * @return the my expectation
	 * @throws com.vijayan.profile.NoSuchMyExpectationException if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation findByPrimaryKey(long exceptationId)
		throws NoSuchMyExpectationException, SystemException {
		MyExpectation myExpectation = fetchByPrimaryKey(exceptationId);

		if (myExpectation == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + exceptationId);
			}

			throw new NoSuchMyExpectationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				exceptationId);
		}

		return myExpectation;
	}

	/**
	 * Finds the my expectation with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the my expectation to find
	 * @return the my expectation, or <code>null</code> if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the my expectation with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param exceptationId the primary key of the my expectation to find
	 * @return the my expectation, or <code>null</code> if a my expectation with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyExpectation fetchByPrimaryKey(long exceptationId)
		throws SystemException {
		MyExpectation myExpectation = (MyExpectation)EntityCacheUtil.getResult(MyExpectationModelImpl.ENTITY_CACHE_ENABLED,
				MyExpectationImpl.class, exceptationId, this);

		if (myExpectation == null) {
			Session session = null;

			try {
				session = openSession();

				myExpectation = (MyExpectation)session.get(MyExpectationImpl.class,
						new Long(exceptationId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (myExpectation != null) {
					cacheResult(myExpectation);
				}

				closeSession(session);
			}
		}

		return myExpectation;
	}

	/**
	 * Finds all the my expectations.
	 *
	 * @return the my expectations
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyExpectation> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the my expectations.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of my expectations to return
	 * @param end the upper bound of the range of my expectations to return (not inclusive)
	 * @return the range of my expectations
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyExpectation> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the my expectations.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of my expectations to return
	 * @param end the upper bound of the range of my expectations to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of my expectations
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyExpectation> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<MyExpectation> list = (List<MyExpectation>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;
				String sql = null;

				if (orderByComparator != null) {
					query = new StringBundler(2 +
							(orderByComparator.getOrderByFields().length * 3));

					query.append(_SQL_SELECT_MYEXPECTATION);

					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);

					sql = query.toString();
				}
				else {
					sql = _SQL_SELECT_MYEXPECTATION;
				}

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<MyExpectation>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<MyExpectation>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<MyExpectation>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the my expectations from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (MyExpectation myExpectation : findAll()) {
			remove(myExpectation);
		}
	}

	/**
	 * Counts all the my expectations.
	 *
	 * @return the number of my expectations
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_MYEXPECTATION);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the my expectation persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.vijayan.profile.model.MyExpectation")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<MyExpectation>> listenersList = new ArrayList<ModelListener<MyExpectation>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<MyExpectation>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	@BeanReference(type = ProfilePersistence.class)
	protected ProfilePersistence profilePersistence;
	@BeanReference(type = MyExpectationPersistence.class)
	protected MyExpectationPersistence myExpectationPersistence;
	@BeanReference(type = FamilyMemberPersistence.class)
	protected FamilyMemberPersistence familyMemberPersistence;
	@BeanReference(type = HoroscopePersistence.class)
	protected HoroscopePersistence horoscopePersistence;
	@BeanReference(type = MyPhotoPersistence.class)
	protected MyPhotoPersistence myPhotoPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_MYEXPECTATION = "SELECT myExpectation FROM MyExpectation myExpectation";
	private static final String _SQL_COUNT_MYEXPECTATION = "SELECT COUNT(myExpectation) FROM MyExpectation myExpectation";
	private static final String _ORDER_BY_ENTITY_ALIAS = "myExpectation.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MyExpectation exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(MyExpectationPersistenceImpl.class);
}